<template>
  <div class="auth-container">
    <div class="auth-card">
      <div class="auth-header">
        <h2>登录</h2>
        <p>欢迎回来，请登录您的账号</p>
      </div>

      <el-form 
        ref="loginFormRef"
        :model="loginForm" 
        :rules="loginRules"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名"
            :disabled="loading"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
            :disabled="loading"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-button 
          type="primary" 
          native-type="submit"
          :loading="loading"
          style="width: 100%"
        >
          登录
        </el-button>
      </el-form>

      <div class="auth-footer">
        <el-link type="primary" :underline="false">忘记密码?</el-link>
        <el-link type="primary" :underline="false">注册账号</el-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores";

const router = useRouter();
const userStore = useUserStore();

const loginForm = ref({
  username: "zhangsan",
  password: "123456",
});

const loginRules = ref<FormRules>({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
});

const loading = ref(false);
const loginFormRef = ref<FormInstance>();

const handleLogin = async () => {
  try {
    loading.value = true;
    await loginFormRef.value?.validate();

    const result = await userStore.login(loginForm.value);
    
    if (result.success) {
      ElMessage.success("登录成功");
      // 登录成功后跳转到主页
      router.push("/");
    } else {
      ElMessage.error(result.error || "登录失败");
    }
  } catch (error: any) {
    ElMessage.error(error.message || "登录失败");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;

  .auth-card {
    width: 400px;
    padding: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    background-color: #fff;

    .auth-header {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        margin: 0;
        font-size: 24px;
        color: #303133;
      }

      p {
        margin: 10px 0 0;
        font-size: 14px;
        color: #909399;
      }
    }

    .auth-form {
      .login-btn {
        width: 100%;
      }
    }

    .auth-footer {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
    }
  }
}
</style>
